package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.controller.admin.back;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.controller.admin.back.vo.BackPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.controller.admin.back.vo.BackVo;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.dal.dataobject.back.BackDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.dal.mysql.back.BackMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.back.service.back.BackService;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.dal.mysql.order.OrderMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.order.kuaidi.KuaiDiProvider;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 退/换货")
@RestController
@RequestMapping("/qd/back")
@Validated
@Transactional
public class BackController {

    @Resource
    private BackService backService;
    @Resource
    BackMapper backMapper;
    @Resource
    OrderMapper orderMapper;

    //商家发货
    @PostMapping("delivery")
    public CommonResult delivery(@RequestBody BackVo vo){
        BackDO backDO = backMapper.selectById(vo.getId());
        if (backDO==null)
            throw new ServiceException(500,"信息不存在,请刷新");

        if (backDO.getRefundStatus()!=6)
            throw new ServiceException(500,"未出与商家驳回状态,请刷新");

        backDO.setRefundStatus(7);
        backDO.setBusinLogistic(vo.getVo());
        backDO.setBusinTime(LocalDateTime.now());
        backMapper.updateById(backDO);
        return CommonResult.success("");
    }

    @GetMapping("/findLogistics")
    @Operation(summary = "查看发货物流")
    public CommonResult findLogistics(Integer id) throws IllegalAccessException {
        BackDO backDO = backMapper.selectById(id);
        if (backDO==null)
            throw new ServiceException(500,"无此数据,请刷新");
        if (backDO.getLogistic()==null)
            throw new ServiceException(500,"未添加物流");

        return success(KuaiDiProvider.getKuaiDiInfo(backDO.getLogistic().getCode(),backDO.getLogistic().getNum()));
    }

    @GetMapping("/findBusinLogistics")
    @Operation(summary = "查看商家发货物流")
    public CommonResult findBusinLogistics(Integer id) throws IllegalAccessException {
        BackDO backDO = backMapper.selectById(id);
        if (backDO==null)
            throw new ServiceException(500,"无此数据,请刷新");
        if (backDO.getLogistic()==null)
            throw new ServiceException(500,"未添加物流");

        return success(KuaiDiProvider.getKuaiDiInfo(backDO.getBusinLogistic().getCode(),backDO.getBusinLogistic().getNum()));
    }

    @GetMapping("confirm")
    @Operation(summary = "确认按钮")
    public CommonResult confirm(Integer id,Integer status) throws Exception {
        BackDO backDO = backMapper.selectById(id);
        if (backDO==null)
            throw new ServiceException(500,"数据不存在,请刷新");
        if (status==1)
            throw new ServiceException(500,"请选择确认状态,当前确认状态是未审核");

        if (backDO.getRefundStatus()!=4)
            throw new ServiceException(500,"用户未发货,还不能点击确认按钮");

        //退货流程
        if (backDO.getType()==1){
            int refundStatus=5;
            if (status==3)
                refundStatus=6;

            backDO.setConfirmStatus(status);
            backDO.setRefundStatus(refundStatus);
            //如果通过触发退款
            if (status==2)
                backService.refund(id);
            backMapper.updateById(backDO);
        }else {
            //换货流程
            backDO.setConfirmStatus(status);
            backDO.setRefundStatus(6);
            //如果通过触发退款
            if (status==2)
                backService.refund(id);
            backMapper.updateById(backDO);
        }
        return success("");
    }

    @GetMapping("auditing")
    @Operation(summary = "审核按钮")
    public CommonResult auditing(Integer id,Integer status) throws Exception {
        BackDO backDO = backMapper.selectById(id);
        if (backDO==null)
            throw new ServiceException(500,"数据不存在,请刷新");
        if (status==1)
            throw new ServiceException(500,"请选择审核状态,当前审核状态是未审核");


        switch (backDO.getType()){
            //退货
            case 1:
                //处于待发货
                if (backDO.getOrderStatus()==2){
                    backDO.setStatus(status);
                    if (backDO.getRefundStatus()!=1)
                        throw new ServiceException(500,"订单未处于商家待审核");

                    //修改退货状态
                    if (status==2){
                        //修改退款状态为退款已完成
                        backDO.setRefundStatus(5);
                        backMapper.updateById(backDO);
                        //退款功能
                        backService.refund(backDO.getId());
                    }else {
                        //退货失败 订单返回原来的状态
                        backDO.setRefundStatus(3);
                        backMapper.updateById(backDO);

                        OrderDO orderDO = orderMapper.selectById(backDO.getOrderId());
                        orderDO.setOrderStatus(backDO.getOrderStatus());
                        orderMapper.updateById(orderDO);
                    }
                }else {
                    //处于已发货
                    backDO.setStatus(status);
                    if (backDO.getRefundStatus()!=1)
                        throw new ServiceException(500,"订单未处于商家待审核");

                    int refundStatus=3;
                    if (status==3)
                        refundStatus=2;
                    //修改退款状态
                    backDO.setRefundStatus(refundStatus);
                    backMapper.updateById(backDO);
                }
                break;
            case 2:
                //处于已发货
                backDO.setStatus(status);
                if (backDO.getRefundStatus()!=1)
                    throw new ServiceException(500,"订单未处于商家待审核");

                int refundStatus=3;
                if (status==3)
                    refundStatus=2;
                //修改退款状态
                backDO.setRefundStatus(refundStatus);
                backMapper.updateById(backDO);
                break;
        }

        return success("");
    }

    @GetMapping("/get")
    @Operation(summary = "获得退/换货")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:back:query')")
    public CommonResult<BackDO> getBack(@RequestParam("id") Integer id) {
        return success(backService.getBack(id));
    }


    @GetMapping("/page")
    @Operation(summary = "获得退/换货分页")
    @PreAuthorize("@ss.hasPermission('qd:back:query')")
    public CommonResult<PageResult<BackDO>> getBackPage(@Valid BackPageReqVO pageVO) {
        PageResult<BackDO> backPage = backService.getBackPage(pageVO);
        List<BackDO> list = backPage.getList();
        for (BackDO backDO : list) {
            String orderCode="";
            OrderDO orderDO = orderMapper.selectById(backDO.getOrderId());
            if (orderDO!=null)
                orderCode=orderDO.getOrderCode();
            backDO.setOrderCode(orderCode);
        }
        backPage.setList(list);
        return success(backPage);
    }



}
